package com.eqxiu.ieditor.weixin.config;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;

import com.eqxiu.ieditor.commons.json.JsonMapper;

import redis.clients.jedis.JedisPoolConfig;

@Configuration
public class RedisCacheConfig extends CachingConfigurerSupport {

	@Value("${spring.redis.host}")
	private String host;

	@Value("${spring.redis.port}")
	private int port;

	@Value("${spring.redis.database}")
	private int database;

	@Value("${spring.redis.password}")
	private String password;

	@Value("${spring.redis.pool.max-active}")
	private int maxTotal;

	@Value("${spring.redis.pool.max-idle}")
	private int maxIdle;

	@Value("${spring.redis.pool.min-idle}")
	private int minIdle;

	@Value("${spring.redis.pool.max-wait}")
	private long maxWaitMillis;

	@Value("${spring.redis.pool.time-between-eviction-runs-millis}")
	private long timeBetweenEvictionRunsMillis;

	@Value("${spring.redis.pool.min-evictable-idle-timemillis}")
	private long minEvictableIdleTimeMillis;

	@Value("${spring.redis.sentinel.master}")
	private String masterName;

	@Value("${spring.redis.sentinel.nodes}")
	private String sentinels;

	@Bean
	RedisConnectionFactory jedisConnectionFactory() {
		JedisPoolConfig poolConfig = new JedisPoolConfig();
		poolConfig.setMaxTotal(maxTotal);
		poolConfig.setMaxWaitMillis(maxWaitMillis);
		poolConfig.setMaxIdle(maxIdle);
		poolConfig.setTestOnBorrow(false);
		poolConfig.setTestWhileIdle(true);
		poolConfig.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
		poolConfig.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);

		RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration(masterName,
				StringUtils.commaDelimitedListToSet(sentinels));

		JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory(sentinelConfig, poolConfig);
		redisConnectionFactory.setHostName(host);
		redisConnectionFactory.setPort(port);
		redisConnectionFactory.setDatabase(database);
		redisConnectionFactory.setPassword(password);

		redisConnectionFactory.setUsePool(true);
		redisConnectionFactory.afterPropertiesSet();

		StringRedisTemplate template = new StringRedisTemplate(redisConnectionFactory);
		return redisConnectionFactory;
	}

	@Bean
	StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
		return new StringRedisTemplate(connectionFactory);
	}

	@Bean
	ObjectMapper objectMapper() {
		return JsonMapper.getInstance().getObjectMapper();
	}

}
